<template>
  <van-button
    v-if="is_followed"
    class="follow-btn"
    round
    :loading="isFollowLoading"
    size="small"
    @click="onFollow"
    >已关注</van-button
  >
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    :loading="isFollowLoading"
    size="small"
    icon="plus"
    @click="onFollow"
    >关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  model: {
    prop: 'is_followed',
    event: 'update-follow'
  },
  props: {
    is_followed: {
      type: Boolean,
      required: true
    },
    user_id: {
      type: [Number, String, Object],
      required: true
    }
  },
  data() {
    return {
      isFollowLoading: false
    }
  },
  created() {},
  mounted() {},
  methods: {
    async onFollow() {
      this.isFollowLoading = true
      try {
        if (this.is_followed) {
          await deleteFollow(this.user_id)
        } else {
          await addFollow(this.user_id)
        }
        this.$emit('update-follow', !this.is_followed)
      } catch (err) {
        let message = '操作失败,请重试'
        if (err.response && err.response.status === 400) {
          message = '不能自己关注自己'
        }
        this.$toast(message)
      }
      this.isFollowLoading = false
    }
  }
}
</script>

<style scoped lang="less"></style>
